package org.accp.baodan.service.impl;

import entity.warranty_info;
import entity.warranty_info_Date;
import org.accp.baodan.dto.PageBean;
import org.accp.baodan.dto.warrantydto;
import org.accp.baodan.mapper.Baodanmapper;
import org.accp.baodan.service.Baodanservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
@Service
public class Baodanserviceimpl implements Baodanservice {
    @Resource
    private Baodanmapper baodanmapper;
    @Override
    public PageBean baodanselect(PageBean pb) {
        int page=pb.getPageIndex();
        int pagesize=pb.getPageSize();
        pb.setPageIndex((page-1)*pagesize+1);
        pb.setPageSize(page*pagesize);
        List<warranty_info> list=baodanmapper.baodanselect(pb);
        List<warranty_info> count=baodanmapper.nobaodanselect(pb);
        PageBean pageb=new PageBean(page,pagesize,count.size());
        System.out.println(pb.getWarrantyInfo().getShenhe());
        pageb.setData(list);
        return pageb;
    }

    @Override
    public void baodaninsert(warrantydto warrantydto1) {
        String shangyexianid=null;
        //续保先删除原有的数据然后插入修改新的数据
        if(warrantydto1.getWarrantyInfo().get(0).getWarranty_state().equals("续保")){
            String jiaoqiangxiandanhao=baodanmapper.baodanhaochaxun(warrantydto1.getWarrantyInfo().get(0).getLicense_number());
            baodanmapper.baodanshanchu(warrantydto1.getWarrantyInfo().get(0).getLicense_number());
            baodanmapper.baodanshanchu1(warrantydto1.getWarrantyInfo().get(0).getWarranty_number());
            warrantydto1.getWarrantyInfo().get(1).setWarranty_number(jiaoqiangxiandanhao);
        }
        //使用循环分别插入商业险和交强险
        for (int i=0;i<warrantydto1.getWarrantyInfo().size();i++) {
            System.out.println(warrantydto1.warrantyInfo.get(i));
            if(warrantydto1.getWarrantyInfo().get(i).getWarranty_type().equals("商业险")){
                shangyexianid=warrantydto1.getWarrantyInfo().get(i).getWarranty_number();
            }
            baodanmapper.baodaninsert(warrantydto1.getWarrantyInfo().get(i));
        }
        //插入商业险下面的基本险
        for(int i=0;i<warrantydto1.getWarrantyInfoData().size();i++) {
            warrantydto1.getWarrantyInfoData().get(i).setWarranty_number(shangyexianid);
            baodanmapper.baodan2insert(warrantydto1.getWarrantyInfoData().get(i));
        }
    }


    @Override
    public void baodan2insert(warranty_info_Date warrantyInfoData) {

    }

    @Override
    public void baodanupdate(warranty_info warrantyInfo) {

    }

    @Override
    public warranty_info chepaiselect(String shen) {
        return baodanmapper.chepaiselect(shen);
    }

    @Override
    public void updatashenhe(warranty_info warranty_info) {
        baodanmapper.updatashenhe(warranty_info);
    }
}
